import { fetchData } from '.';
import * as cheerio from 'cheerio';

export async function scrapePoemType(url: string) {
  // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
  try {
    const html: string | Buffer = await fetchData(url);
    if (html) {
      const $ = cheerio.load(html);

      const results = [];
      const mainElement = $('.mt-4.space-y-8.p-4'); // 获取目标节点
      mainElement.children('div').each((index, element) => {
        const name = $(element).find('h2.prose-h2').text(); // 获取h2内容
        const description = $(element)
          .find('p a')
          .map((i, el) => $(el).text())
          .get()
          .join(', '); // 获取a链接文本并拼接

        results.push({ name, description: description || null }); // 添加到结果数组
      });

      // eslint-disable-next-line @typescript-eslint/no-unsafe-return
      return results;
    }
  } catch (error) {
    throw Error(error);
  }
}
